package pay

import java.util.function.{Function, Predicate}

import com.alipay.api.AlipayClient
import com.trjcn.wxpay.service.WXPayService
import config.{AliPayProperties, WXPayProperties}
import dto.req.{NotifyReqDTO, PayOrderReqDTO}
import dto.resp.PayOrderRespDTO
import factory.{ActionFactory, PayAction}
import org.slf4j.LoggerFactory
import utils.AssertUtils

abstract class BasePayAction extends PayAction {

  protected val log = LoggerFactory.getLogger(this.getClass)

  protected var alipayClient:AlipayClient = null

  protected var aliPayProperties:AliPayProperties = null

  protected var wxPayProperties:WXPayProperties = null

  protected var wxPayService:WXPayService = null


  override def setProperties(bizCode: String): Unit = {
    aliPayProperties = ActionFactory.getAliPayProperties(bizCode)
    wxPayProperties =ActionFactory.getWXPayProperties(bizCode)
    wxPayService = wxPayProperties.getWXPayService
    alipayClient = aliPayProperties.getAlipayClient
  }


  override def checkParams[T <: PayOrderReqDTO](req: T): Unit = {
    AssertUtils.notNull(req.getPayChannel, "payChannel 不能为空")
    AssertUtils.notNull(req.getBizOrderNo, "bizOrderNo 不能为空")
    AssertUtils.notNull(req.getTxType, "txType 不能为空")
    AssertUtils.notNull(req.getUserId, "userId 不能为空")
    AssertUtils.notNull(req.getPayAmount, "payAmount 不能为空")
  }


  override def payTransaction(req: PayOrderRespDTO, function: Function[PayOrderRespDTO, PayOrderRespDTO]): PayOrderRespDTO = {
    function.apply(req)
  }

  override def pay[T <: PayOrderReqDTO](req: T): Unit = {
    throw new RuntimeException()
  }

  override def callback(req: NotifyReqDTO, predicate: Predicate[NotifyReqDTO]): Boolean = {
    predicate.test(req)
  }
}
